//
//  PosterCollectionViewCell.m
//  MyMovie
//
//  Created by Weiwei on 16/3/1.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "PosterCollectionViewCell.h"
#import "RatingView.h"
//设置宏
#define kCellW self.contentView.bounds.size.width
#define kCellH self.contentView.bounds.size.height

@interface PosterCollectionViewCell (){
    //把所有的控件都全局
//    UIImageView* _frontView;
//    UIView* _reverseView;
    
    
    UILabel* _cnLabel;
    UIImageView* _imageView;
    RatingView* _ratingView;
}

@end



@implementation PosterCollectionViewCell

//步骤一
//1.注册单元格产生cell一定会走
-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    //1.创建一个正面试图
    [self createFrontView];
    
    //2.创建一个反面试图，隐藏
    [self createReverseView];

    //3.将当前cell的正反值设为
    self.isFront =YES;
    
    
    return self;
}
#pragma -mark 创建一个正面视图
-(void)createFrontView{
    
    //1.创建一个imageView
     _frontView =  [[UIImageView alloc]initWithFrame:self.contentView.bounds];
    [self.contentView addSubview:_frontView];
    
}

#pragma -mark 创建一个反面的试图
-(void)createReverseView{
    
    //1.创建一个反面View
    _reverseView = [[UIView alloc]initWithFrame:self.contentView.bounds];
    _reverseView.backgroundColor = [UIColor whiteColor];
    
    //2.一开始是隐藏
    _reverseView.hidden = YES;
    [self.contentView addSubview:_reverseView];
    
    //--------------反面视图的组成部分-------
    //1.创建一个电影名字的lable
    _cnLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, kCellW, 50)];
    _cnLabel.textAlignment = NSTextAlignmentCenter;
    _cnLabel.font = [UIFont systemFontOfSize:30];
//    _cnLabel.text = @"潜女幽魂";
    [_reverseView addSubview:_cnLabel];
    
    //2.创建一个imageView
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 70, kCellW - 40, kCellH - 70 - 70)];
    _imageView.backgroundColor = [UIColor redColor];
    [_reverseView addSubview:_imageView];
    
    
    //3.增加一个星星试图
    _ratingView = [[RatingView alloc]initWithFrame:CGRectMake((kCellW - 150)/2.0, kCellH - 50, 150, 30)];
    [_reverseView addSubview:_ratingView];
    

}
#pragma -mark 步骤二 重写setModel,进行属性的复制
-(void)setModel:(HomeModel *)model{
    
    //将model保存一份
    _model = model;
    
    //1.正面
    [_frontView setImageWithURL:[NSURL URLWithString:model.img]];
    
    //2.反面
    _cnLabel.text = _model.titleCn;
    [_imageView setImageWithURL:[NSURL URLWithString:model.img]];
    _ratingView.rating = [_model.ratingFinal floatValue];
    
    
    
}

//-------------------封装一个反转方法-------------

-(void)filpView{
    
    //1.需要一个属性用来判断cell的正反
    
    if (self.isFront == YES) {
        
        //当一开始为正面的时候
        //隐藏第一面
        _frontView.hidden = YES;
        //显示反面
        _reverseView.hidden =NO;
        
    }else{
        //如果一开始是反面
        //反面开始隐藏，正面显示
        _frontView.hidden = NO;
        _reverseView.hidden = YES;
        

    }

#pragma -mark  ....三目运算符，相当于进行_ISfront判断，进行设置反转的方向
    [UIView animateWithDuration:.3 animations:^{
    
        [UIView setAnimationTransition:_isFront?UIViewAnimationTransitionFlipFromLeft:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
    
        
    }];
    
    
    
    
    
    
    
    //改变状态值
    _isFront = !_isFront;
    
    
    
}






@end
